package com.orange.score.service;

import android.os.AsyncTask;
import android.util.Log;
import com.orange.common.android.activity.PPActivity;
import com.orange.score.model.config.ConfigManager;
import com.orange.score.model.index.CompanyManager;
import com.orange.score.model.index.OddsManager;
import com.orange.score.model.league.LeagueManager;
import com.orange.score.model.match.MatchManager;
import com.orange.score.network.ResultCodeType;
import com.orange.score.network.ScoreNetworkRequest;
import com.orange.score.utils.FileService;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

public class IndexService
{
  public static final int DEFAULT_INTERVAL = 10;
  public static final String TAG = "IndexCompanyService";
  private CompanyManager companyManager;
  private LeagueManager leagueManager;
  Timer liveOddsUpdateTimer;
  LiveUpdateOddsTask liveUpdateOddsTask;
  private MatchManager matchManager;
  private OddsManager oddsManager = new OddsManager();
  List<OddsUpdateChangeCallBack> oddsUpdateChangeObserverList;

  public IndexService()
  {
    this.companyManager = new CompanyManager();
    this.leagueManager = new LeagueManager();
    this.matchManager = new MatchManager();
    this.liveUpdateOddsTask = null;
    this.liveOddsUpdateTimer = null;
    this.oddsUpdateChangeObserverList = new ArrayList();
  }

  private void liveOddsUpate()
  {
    this.liveUpdateOddsTask = new LiveUpdateOddsTask(this, null);
    LiveUpdateOddsTask localLiveUpdateOddsTask = this.liveUpdateOddsTask;
    String[] arrayOfString = new String[1];
    arrayOfString[0] = "";
    localLiveUpdateOddsTask.execute(arrayOfString);
  }

  private void startLiveOddsUpdateTimer(boolean paramBoolean)
  {
    if (this.liveOddsUpdateTimer != null)
    {
      this.liveOddsUpdateTimer.cancel();
      this.liveOddsUpdateTimer = null;
    }
    int i = 10;
    if (paramBoolean)
    {
      i = 0;
      this.liveOddsUpdateTimer = new Timer();
      this.liveOddsUpdateTimer.schedule(new TimerTask(this)
      {
        public void run()
        {
          Log.d("IndexCompanyService", "Fire live odds update timer");
          IndexService.access$4(this.this$0);
        }
      }
      , i * 1000);
      return;
    }
    try
    {
      int j = Integer.parseInt(ConfigManager.getInstance().getRefreshInterval());
      i = j;
    }
    catch (Exception localException)
    {
    }
  }

  private void stopLiveOddsUpdateTimer()
  {
    if (this.liveOddsUpdateTimer != null)
    {
      this.liveOddsUpdateTimer.cancel();
      this.liveOddsUpdateTimer = null;
    }
  }

  public void addOddsLiveUpdateObserver(OddsUpdateChangeCallBack paramOddsUpdateChangeCallBack)
  {
    this.oddsUpdateChangeObserverList.add(paramOddsUpdateChangeCallBack);
  }

  public void addOddsLiveUpdateObserverAsFirst(OddsUpdateChangeCallBack paramOddsUpdateChangeCallBack)
  {
    this.oddsUpdateChangeObserverList.add(0, paramOddsUpdateChangeCallBack);
  }

  public CompanyManager getCompanyManager()
  {
    return this.companyManager;
  }

  public LeagueManager getLeagueManager()
  {
    return this.leagueManager;
  }

  public MatchManager getMatchManager()
  {
    return this.matchManager;
  }

  public OddsManager getOddsManager()
  {
    return this.oddsManager;
  }

  public void loadCompanyData(PPActivity paramPPActivity, IndexServiceCallBack paramIndexServiceCallBack)
  {
    LoadIndexCompanyTask localLoadIndexCompanyTask = new LoadIndexCompanyTask(this, paramPPActivity, paramIndexServiceCallBack);
    String[] arrayOfString = new String[1];
    arrayOfString[0] = "";
    localLoadIndexCompanyTask.execute(arrayOfString);
  }

  public void loadOddsData(PPActivity paramPPActivity, IndexServiceCallBack paramIndexServiceCallBack, String paramString1, String paramString2)
  {
    LoadOddsTask localLoadOddsTask = new LoadOddsTask(this, paramPPActivity, paramIndexServiceCallBack);
    String[] arrayOfString = new String[2];
    arrayOfString[0] = paramString1;
    arrayOfString[1] = paramString2;
    localLoadOddsTask.execute(arrayOfString);
  }

  public void removeOddsLiveUpdateObserver(OddsUpdateChangeCallBack paramOddsUpdateChangeCallBack)
  {
    this.oddsUpdateChangeObserverList.remove(paramOddsUpdateChangeCallBack);
  }

  private class LiveUpdateOddsTask extends AsyncTask<String, Void, List<String[]>>
  {
    protected List<String[]> doInBackground()
    {
      ArrayList localArrayList;
      int i;
      Object localObject;
      String[] arrayOfString1 = ScoreNetworkRequest.getOddsLiveChange(IndexService.access$0(this.this$0).getOddsType());
      if (arrayOfString1 == null)
        localObject = null;
      while (true)
      {
        return localObject;
        localArrayList = new ArrayList();
        i = 0;
        if (i < arrayOfString1.length)
          break;
        localObject = localArrayList;
      }
      if (arrayOfString1[i].length() == 0);
      while (true)
      {
        String[] arrayOfString2;
        while (true)
        {
          while (true)
            ++i;
          arrayOfString2 = arrayOfString1[i].split("\\^");
          if ((arrayOfString2 != null) && (arrayOfString2.length >= 5))
            break;
          Log.w("IndexCompanyService", "LiveUpdateOdds but fields count not enough, count = " + arrayOfString2.length);
        }
        localArrayList.add(arrayOfString2);
      }
    }

    protected void onPostExecute()
    {
      int i;
      int j;
      super.onPostExecute(paramList);
      Set localSet = IndexService.access$1(this.this$0).getOddsUpdateSet(paramList, IndexService.access$0(this.this$0).getOddsType());
      Log.d("IndexCompanyService", "LiveUpdateOddsTask onPostExecute");
      if ((paramList != null) && (paramList.size() > 0))
      {
        i = this.this$0.oddsUpdateChangeObserverList.size();
        j = 0;
      }
      while (true)
      {
        if (j >= i)
        {
          IndexService.access$2(this.this$0, false);
          return;
        }
        ((OddsUpdateChangeCallBack)this.this$0.oddsUpdateChangeObserverList.get(j)).notifyOddsUpdate(localSet);
        ++j;
      }
    }
  }

  private class LoadIndexCompanyTask extends AsyncTask<String, Void, String[]>
  {
    PPActivity activity;
    IndexServiceCallBack callback;

    public LoadIndexCompanyTask(, PPActivity paramPPActivity, IndexServiceCallBack paramIndexServiceCallBack)
    {
      this.callback = paramIndexServiceCallBack;
      this.activity = paramPPActivity;
    }

    protected String[] doInBackground()
    {
      Object localObject;
      FileService localFileService = new FileService(this.activity);
      try
      {
        String str = localFileService.read("odds_company.txt");
        Log.d("IndexCompanyService", "Get odds company from odds_company.txt");
        String[] arrayOfString2 = str.split("\\!");
        localObject = arrayOfString2;
        return localObject;
      }
      catch (Exception localException)
      {
        String[] arrayOfString1;
        StringBuilder localStringBuilder;
        int i;
        while (true)
        {
          while (true)
          {
            Log.d("IndexCompanyService", "Get odds company from network");
            arrayOfString1 = ScoreNetworkRequest.getIndexCompany();
            if (arrayOfString1 != null)
              break;
            localObject = null;
          }
          localStringBuilder = new StringBuilder();
          i = 0;
          if (i < arrayOfString1.length)
            break;
          localFileService.save("odds_company.txt", localStringBuilder.toString());
          localObject = arrayOfString1;
        }
        localStringBuilder.append("!").append(arrayOfString1[i]);
        ++i;
      }
    }

    protected void onPostExecute()
    {
      super.onPostExecute(paramArrayOfString);
      this.activity.hideDialog();
      if ((paramArrayOfString != null) && (paramArrayOfString.length > 0))
      {
        IndexService.access$0(this.this$0).updateCompanyDataFromStringList(paramArrayOfString);
        this.callback.loadAllCompanyFinish(ResultCodeType.SUCCESS);
      }
    }

    protected void onPreExecute()
    {
      super.onPreExecute();
      this.activity.showProgressDialog("", "加载数据中", this);
    }
  }

  private class LoadOddsTask extends AsyncTask<String, Void, String[]>
  {
    PPActivity activity;
    IndexServiceCallBack callBack;

    public LoadOddsTask(, PPActivity paramPPActivity, IndexServiceCallBack paramIndexServiceCallBack)
    {
      this.activity = paramPPActivity;
      this.callBack = paramIndexServiceCallBack;
    }

    protected String[] doInBackground()
    {
      return ScoreNetworkRequest.getOddsData(paramArrayOfString[0], paramArrayOfString[1], ConfigManager.getLanguage(), IndexService.access$1(this.this$0).getFilterScoreType(), IndexService.access$0(this.this$0).getOddsType());
    }

    protected void onPostExecute()
    {
      super.onPostExecute(paramArrayOfString);
      this.activity.hideDialog();
      if (paramArrayOfString == null)
        Log.w("IndexCompanyService", "load odds data return null");
      while (true)
      {
        while (true)
        {
          return;
          if (paramArrayOfString.length >= 3)
            break;
          Log.w("IndexCompanyService", "get Odds data but length not enough, length = " + paramArrayOfString.length);
        }
        String str1 = "";
        String str2 = "";
        String str3 = "";
        if (paramArrayOfString.length >= 3)
        {
          str1 = paramArrayOfString[0];
          str2 = paramArrayOfString[1];
          str3 = paramArrayOfString[2];
        }
        String[] arrayOfString1 = str1.split("\\!");
        IndexService.access$3(this.this$0).updateDataFromStringList(arrayOfString1, 2);
        String[] arrayOfString2 = str2.split("\\!");
        IndexService.access$1(this.this$0).updateOddsMatchDataFromStringList(arrayOfString2, IndexService.access$3(this.this$0));
        String[] arrayOfString3 = str3.split("\\!");
        IndexService.access$1(this.this$0).updateOddsPeilvDataFromStringList(arrayOfString3, IndexService.access$0(this.this$0).getOddsType());
        this.callBack.loadOddsDataFinish(ResultCodeType.SUCCESS);
        IndexService.access$2(this.this$0, false);
      }
    }

    protected void onPreExecute()
    {
      super.onPreExecute();
      this.activity.showProgressDialog("", "加载数据中", this);
    }
  }
}